package net.tv90.bfmccore.game.ai;

import net.tv90.bfmccore.game.GameBase;
import net.tv90.bfmccore.game.GameManager;
import net.tv90.bfmccore.game.impl.ConquestGame;
import net.tv90.bfmccore.game.impl.TDMGame;
import net.tv90.bfmccore.game.impl.cq.ConquestArea;
import net.tv90.bfmccore.helper.FactionHelper;
import net.tv90.bfmccore.util.vector.Vector3i;
import org.bitterorange.flansmodslain.FlansModAPI;
import org.bitterorange.flansmodslain.models.SoldierDeathEvent;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

public class AISystem implements Listener {

    public void tick(int tps) {
        GameBase game = GameManager.getInstance().currentGame;
        if (game == null || game.isEnd()) return;
        if (GameBase.inGamePlayerList.size() == 0) return;

        int frame = GameBase.frame;
        if (frame % (game.timeToSpawnAi * tps) == 0) {
            firstFactionSpawnAi(game);
            secondFactionSpawnAi(game);
        }
    }


    public void firstFactionSpawnAi(GameBase gameBase) {
        if (gameBase.firstFactionCurrentAiNum >= gameBase.maxAi) return;
        if (gameBase instanceof TDMGame) {
            TDMGame game = (TDMGame) gameBase;
            List<Vector3i> list = game.firstFactionSpawnPointList;
            Vector3i vec = list.get(ThreadLocalRandom.current().nextInt(list.size()));
            gameBase.firstFactionCurrentAiNum++;
            FlansModAPI.spawnAI(Bukkit.getWorld(game.mapName), vec.x, vec.y, vec.z, FactionHelper.getScoreBoardName(FactionHelper.EnumCountry.valueOf(game.firstFactionName)));
        } else if (gameBase instanceof ConquestGame) {
            ConquestGame game = (ConquestGame) gameBase;
            game.conquestAreaList.forEach((area, v) -> {
                if (area.canCapture
                        && area.currentFaction.equals(game.firstFactionName)
                        && area.state != ConquestArea.ConquestAreaState.NEUTRALIZED) {
                    List<Vector3i> list =
                            area.state == ConquestArea.ConquestAreaState.CAPTURED
                                    ? area.firstFactionSpawn
                                    : area.firstFactionSpawnWhenCapturing;
                    if (gameBase.firstFactionCurrentAiNum >= gameBase.maxAi) return;
                    Vector3i vec = list.get(ThreadLocalRandom.current().nextInt(list.size()));
                    gameBase.firstFactionCurrentAiNum++;
                    FlansModAPI.spawnAI(Bukkit.getWorld(game.mapName), vec.x, vec.y, vec.z, FactionHelper.getScoreBoardName(FactionHelper.EnumCountry.valueOf(game.firstFactionName)));
                }
            });
        }
    }

    public void secondFactionSpawnAi(GameBase gameBase) {
        if (gameBase.secondFactionCurrentAiNum >= gameBase.maxAi) return;
        if (gameBase instanceof TDMGame) {
            TDMGame game = (TDMGame) gameBase;
            List<Vector3i> list = game.secondFactionSpawnPointList;
            Vector3i vec = list.get(ThreadLocalRandom.current().nextInt(list.size()));
            gameBase.secondFactionCurrentAiNum++;
            FlansModAPI.spawnAI(Bukkit.getWorld(game.mapName), vec.x, vec.y, vec.z, FactionHelper.getScoreBoardName(FactionHelper.EnumCountry.valueOf(game.secondFactionName)));
        } else if (gameBase instanceof ConquestGame) {
            ConquestGame game = (ConquestGame) gameBase;
            game.conquestAreaList.forEach((area, v) -> {
                if (area.canCapture
                        && area.currentFaction.equals(game.secondFactionName)
                        && area.state != ConquestArea.ConquestAreaState.NEUTRALIZED) {
                    List<Vector3i> list =
                            area.state == ConquestArea.ConquestAreaState.CAPTURED
                                    ? area.secondFactionSpawn
                                    : area.secondFactionSpawnWhenCapturing;
                    if (gameBase.secondFactionCurrentAiNum >= gameBase.maxAi) return;
                    Vector3i vec = list.get(ThreadLocalRandom.current().nextInt(list.size()));
                    gameBase.secondFactionCurrentAiNum++;
                    FlansModAPI.spawnAI(Bukkit.getWorld(game.mapName), vec.x, vec.y, vec.z, FactionHelper.getScoreBoardName(FactionHelper.EnumCountry.valueOf(game.secondFactionName)));
                }
            });
        }
    }

    @EventHandler
    public void onAiDeath(SoldierDeathEvent event) {
        if(event.isIgnoreDeathCheck()) {
            return;
        }
        GameBase game = GameManager.getInstance().currentGame;
        if (game == null || game.isEnd()) return;
        //if(game instanceof TDMGame) {
        //TDMGame tdmGame = (TDMGame) game;
        String country = FactionHelper.getAICountry(event.getSoldier().getCountry());
        if (country.equals(game.firstFactionName)) {
            game.firstFactionCurrentAiNum = Math.max(0, game.firstFactionCurrentAiNum - 1);
        } else if (country.equals(game.secondFactionName)) {
            game.secondFactionCurrentAiNum = Math.max(0, game.secondFactionCurrentAiNum - 1);
        }
        // }
    }
}
